package com.njtoyo.taxi.admin.service.business.ride.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.mapper.master.entity.OrderManageVo;
import com.njtoyo.taxi.admin.mapper.master.entity.ScanRide;
import com.njtoyo.taxi.admin.mapper.master.entity.ScanRideOrder;
import com.njtoyo.taxi.admin.mapper.master.mapper.ScanRideMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.ScanRideOrderMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.ride.scan_ride.ScanRideDetailsPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.ride.scan_ride.ScanRideListPresenter;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.AdminUserIdentityPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.ride.OrderManageQueryDto;
import com.njtoyo.taxi.admin.rest.wrapper.business.ride.ScanRideWrapper;
import com.njtoyo.taxi.admin.service.business.ride.ScanRideOrderService;
import com.taxi.entity.common.RestResult;
import com.taxi.entity.common.ResultEnum;
import com.njtoyo.taxi.entity.backend.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.util.stream.Collectors.toList;

@Service
public class ScanRideServiceImpl implements ScanRideOrderService {
    @Autowired
    private ScanRideMapper scanRideMapper;
    @Autowired
    private ScanRideOrderMapper scanRideOrderMapper;


    /**
     * 扫码订单列表
     *
     * @param adminUser
     * @param wrapper
     * @return
     */
    @Override
    public RestResult<Page<ScanRideListPresenter>> sweepOrderList(AdminUser adminUser, ScanRideWrapper wrapper) {
        AdminUserIdentityPresenter identity = (AdminUserIdentityPresenter) adminUser.getIdentity();
        switch (adminUser.getAccountType()) {
            case canton:
                wrapper.setCantonIds(identity.getCantonIds());
                break;
            case company:
                wrapper.setCompanyIds(identity.getCompanyIds());
        }
        Page<ScanRideListPresenter> list = scanRideMapper.selectSweepOrderPage(wrapper);
        //扫码订单
        List<Long> ids = list.getRecords().stream()
                .map(ScanRideListPresenter::getId)
                .collect(toList());
        //验证是否存在数据
        if (ObjectUtils.isEmpty(ids)) {
            return RestResult.success(list);
        }
        //扫码订单
        QueryWrapper<ScanRideOrder> scanRideOrderQueryWrapper = new QueryWrapper<>();
        scanRideOrderQueryWrapper.select("id", "scan_ride_id", "amount", "payment_amount", "discount_amount", "driver_earn_amount", "payment_channel", "is_paid", "paid_timestamp");
        scanRideOrderQueryWrapper.in("scan_ride_id", ids);
        List<ScanRideOrder> scanRideOrders = scanRideOrderMapper.selectList(scanRideOrderQueryWrapper);
        for (ScanRideListPresenter e : list.getRecords()) {
            Long id = e.getId();
            for (ScanRideOrder s : scanRideOrders) {
                if (s != null) {
                    if (s.getScanRideId().equals(id)) {
                        e.setAmount(s.getAmount());
                        e.setPaymentAmount(s.getPaymentAmount());
                        e.setDiscountAmount(s.getDiscountAmount());
                        e.setDriverEarnAmount(s.getDriverEarnAmount());
                        e.setPaymentChannel(s.getPaymentChannel());
                        e.setIsPaid(s.getIsPaid());
                        e.setPaidTimestamp(s.getPaidTimestamp());
                    }
                }
            }
        }
        return RestResult.success(list);
    }

    /**
     * 扫码订单详情
     *
     * @param id
     * @return
     */
    @Override
    public RestResult<ScanRideDetailsPresenter> sweepOrderDetails(Long id) {
        ScanRide scanRide = scanRideMapper.selectById(id);
        if (ObjectUtils.isEmpty(scanRide)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        ScanRideDetailsPresenter sweepOrderDetailsPresenter = scanRideMapper.sweepOrderDetails(id);
        return RestResult.success(sweepOrderDetailsPresenter);
    }

    @Override
    public List<OrderManageVo> getSweepOrderAll(AdminUser adminUser, OrderManageQueryDto dto) {
        List<OrderManageVo> list = new ArrayList<>();
        AdminUserIdentityPresenter identity = (AdminUserIdentityPresenter) adminUser.getIdentity();
        switch (adminUser.getAccountType()) {
            case canton:
                dto.setCantonIds(identity.getCantonIds());
                break;
            case company:
                dto.setCompanyIds(identity.getCompanyIds());
        }
        List<ScanRideListPresenter> scanRideListPresenters = scanRideMapper.selectSweepOrderAll(dto);
        if (!ObjectUtils.isEmpty(scanRideListPresenters)) {
            for (ScanRideListPresenter e : scanRideListPresenters) {
                OrderManageVo orderManageVo = new OrderManageVo();
                orderManageVo.setTabName("扫码行程");
                orderManageVo.setId(e.getId());
                //用户信息
                orderManageVo.setPassengerId(e.getCustomerId().toString());
                orderManageVo.setPassengerName(e.getCustomerName());
                orderManageVo.setDriverPhoneNumber(e.getDriverMobileNumber());
                //位置信息
                orderManageVo.setDestination(e.getDestination());
                orderManageVo.setCurrentDriverLocate(e.getCurrentDriverLocate());
                //接单司机
                orderManageVo.setDriverId(e.getDriverId().toString());
                orderManageVo.setDriverName(e.getDriverName());
                orderManageVo.setDriverPhoneNumber(e.getDriverMobileNumber());
                //行政区
                orderManageVo.setAreaName(e.getCantonName());
                //扫码时间
                orderManageVo.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(e.getCreatedAt()));
                //首单立减
                if(null != e.getIsDiscountable()){
                    orderManageVo.setIsDiscountable(e.getIsDiscountable());
                }
                if(null != e.getIsFinished())
                {
                    //是否完成行程
                    orderManageVo.setIsFinished(e.getIsFinished());
                }

                //支付信息
                QueryWrapper<ScanRideOrder> scanRideOrderQueryWrapper = new QueryWrapper<>();
                scanRideOrderQueryWrapper.select("id", "scan_ride_id", "amount", "payment_amount", "discount_amount", "driver_earn_amount", "payment_channel", "is_paid", "paid_timestamp");
                scanRideOrderQueryWrapper.eq("scan_ride_id", e.getId());
                ScanRideOrder scanRideOrder = scanRideOrderMapper.selectOne(scanRideOrderQueryWrapper);
                if(!ObjectUtils.isEmpty(scanRideOrder)){
                    orderManageVo.setAmount(scanRideOrder.getAmount());
                    if(scanRideOrder.getIsPaid()){
                        orderManageVo.setPaidState("已支付");
                        orderManageVo.setPaidThrough(scanRideOrder.getPaymentChannel().getDesc());
                    }else{
                        orderManageVo.setPaidState("未支付");
                    }
                }
                list.add(orderManageVo);
            }
        }
        return list;
    }
}
